# coding=utf-8

import os
import os.path
import shutil
import stat
import sys
from subprocess import Popen, PIPE

# Check command line argument
if len(sys.argv)<4:
    print("Usage: %s RevA RevB #CL_NUM" % sys.argv[0])
    exit(-1)

RevA   = sys.argv[1]
RevB   = sys.argv[2]
CL_NUM = sys.argv[3]

MB_DIRS = ['D:', os.sep, 'dev', 'p4', 'stg_ugl', 'stg', 'PerfTools', 'Microbench']
MB_DIR  = os.path.join(*MB_DIRS)

def ExecCmd(args):
    perforce = Popen(args, stdout = PIPE, stderr = PIPE, cwd = MB_DIR, env=None, universal_newlines=True)
    out, err = perforce.communicate()
    print(out)
    if err:
        print(err)
    return out, err

args = ['git', 'diff', '--name-status', RevA, RevB]
out, err = ExecCmd(args)

CMD_MAP = {
    'A' : 'add',
    'M' : 'edit',
    'D' : 'delete',
}

for x in out.splitlines():
    cmd, file = x.split()
    assert(cmd in CMD_MAP)
    args = ['p4', CMD_MAP[cmd], '-c', CL_NUM, os.path.join(MB_DIR, file)]
    ExecCmd(args)
